<?php
/**
 * @author 
 * @copyright 2014
 */
 
 
class SmsemailModel extends CI_Model
{
    var $_menus = array();
    
	function __construct(){
		parent::__construct('smsemail');
	}
	
	function save1($data,$id){
		$this->db->trans_start();
		if(empty($id)){
			$data['datecreate'] =  gmdate("Y-m-d H:s:i", time() + 7 * 3600);
			$data['usercreate'] = $this->pub->login['username']; 	
			$this->model->insert($data);
		}
		else{
			$data['dateupdate'] =  gmdate("Y-m-d H:s:i", time() + 7 * 3600);
			$data['userudate'] = $this->pub->login['username']; 	
			$this->model->where('id',$id)->update($data);
		}
		$this->db->trans_complete();
		if ($this->db->trans_status() === FALSE){
			return 0;
		}
		else{
			return 1;	
		}
	}
	
	function save($data){
		$this->db->trans_start();
        $login = $this->pub->GetSession('login');
        if(!empty($data['iddonvi'])) {
            $objCq = $this->department_model->loadDepartmentSoap($data['iddonvi']);
            //print_r($objCq); exit();
            $data['madonvi'] = isset($objCq->usercode)?$objCq->usercode:"";
            $data['tendonvi'] = isset($objCq->names)?$objCq->names:"";
        }
        if(!empty($data['idnvcq'])) {
            $objCq = $this->department_model->loadChuyenquan($data['idnvcq']);
            $data['manvcq'] = isset($objCq->ma)?$objCq->ma:"";
            $data['tennvcq'] = isset($objCq->ten)?$objCq->ten:"";
        }        
		if(empty($data['id'])){
			$data['ngaytao'] =  gmdate("Y-m-d H:s:i", time() + 7 * 3600);
			$data['nguoitao'] = $login['taikhoan'];
			$this->model->insert($data);
		}
		else{
			$data['ngaysua'] =  gmdate("Y-m-d H:s:i", time() + 7 * 3600);
			$data['nguoisua'] = $login['taikhoan'];
			$this->model->where('id',$data['id'])->update($data);
		}
		$this->db->trans_complete();
		if ($this->db->trans_status() === FALSE){
			return 0;
		}
		else{
			return 1;	
		}
	}
	
	function delete($str,$item){
		if(!empty($item)){
			$this->model->where('id',$item)->update(array('thungrac'=>1));
			echo 1;
		}
		else{
			$array = explode(',',$str);
			foreach($array as $k=>$id){
				$this->model->where('id',$id)->update(array('thungrac'=>1));	
				echo 1;
			}
		}
	}
    
    function listChutri(){
		$sql = "
				select u.id, u.fullname
				from users u
				left join groups g on u.groups = g.id
				where u.isdelete = 0
				and g.types = 'GD';
		";
		$query = $this->model->query($sql)->execute();
		$arr = array();
		foreach($query as $item){
			$arr[$item->id] = $item->fullname;
		}
		return $arr;
	}
    
    function arr_donvi_soap($login)
    {		
		$sql="SELECT id, ma, ten, loai FROM (`trungtam`) where thungrac=0 ORDER BY thutu";
		$response=$this->pub->ketnoi_sql($sql,'get'); //echo "<pre>"; print_r($response); exit;
		return $response;
    }
    
    function combo_chuyenquan($login)
    {
		$sql="SELECT `id`, `tennhanvien` as ten, taikhoan FROM (`nhanvien`) where `phanmem` LIKE '%,".$this->pub->config['session_name'].",%' and trungtam='25' and tennhanvien<>'P.KH'" ;//lay nhan vien chuyen quan la phong ke hoach//print_r($sql);exit;
		$response=$this->pub->ketnoi_sql($sql,'get');//print_r($sql);exit;
		$combo_chuyenquan=array();	
		for($i=0;$i<count($response);$i++)
		{
			$combo_chuyenquan[$response[$i]['id']]=$response[$i]['taikhoan'].' - '.$response[$i]['ten'];		   
		}
        //print_r($combo_chuyenquan); exit();
		return $combo_chuyenquan;
    }
}